package com.codeoss.module.workflow.service.impl;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.framework.workflow.context.WorkflowContext;
import com.codeoss.framework.workflow.def.NodeDef;
import com.codeoss.framework.workflow.def.WorkflowDef;
import com.codeoss.framework.workflow.service.INodeService;
import com.codeoss.framework.workflow.store.IWorkflowStore;
import com.codeoss.module.workflow.dao.WorkflowTokenMapper;
import com.codeoss.module.workflow.model.WorkflowActivity;
import com.codeoss.module.workflow.model.WorkflowActivityInstance;
import com.codeoss.module.workflow.model.WorkflowProcess;
import com.codeoss.module.workflow.model.WorkflowToken;
import com.codeoss.module.workflow.service.IWorkflowActivityInstanceService;
import com.codeoss.module.workflow.service.IWorkflowActivityService;
import com.codeoss.module.workflow.service.IWorkflowProcessService;
import com.codeoss.module.workflow.service.IWorkflowTokenService;

@Service
public class WorkflowTokenService extends BaseService<WorkflowTokenMapper, WorkflowToken>
		implements IWorkflowTokenService,ApplicationContextAware {
    @Autowired
    private IWorkflowActivityService workflowActivityService;
    @Autowired
    private IWorkflowActivityInstanceService workflowActivityInstanceService;
    @Autowired
    private IWorkflowProcessService workflowProcessService;
    @Autowired
    private IWorkflowStore workflowStore;

    private ApplicationContext applicationContext;

	@Override
	@Transactional
	public void signal(WorkflowToken workflowToken,String result) {
		// TODO Auto-generated method stub
		WorkflowActivityInstance workflowActivityInstance=workflowActivityInstanceService.getById(workflowToken.getWorkflowActivityInstanceId());
		WorkflowActivity workflowActivity=workflowActivityService.getById(workflowActivityInstance.getWorkflowActivityId());
		WorkflowProcess workflowProcess=workflowProcessService.getById(workflowActivityInstance.getWorkflowProcessId());
		WorkflowDef workflowDef=workflowStore.get(workflowProcess.getWorkflowWorkflowId());
		NodeDef nodeDef=workflowDef.getNodeDefById(workflowActivity.getNodeId());
		INodeService nodeService=(INodeService)applicationContext.getBean(nodeDef.getType()+"NodeService");
		
		WorkflowContext workflowContext=new WorkflowContext();
		workflowContext.setWorkflowToken(workflowToken);
		workflowContext.setNodeDef(nodeDef);
		workflowContext.setWorkflowProcess(workflowProcess);
		workflowContext.setWorkflowActivityInstance(workflowActivityInstance);
		workflowContext.setWorkflowActivity(workflowActivity);
		workflowContext.setApproved(result);
		workflowContext.setBizKey(workflowProcess.getBizKey());
		nodeService.end(workflowContext);
	}

	@Override
	@Transactional
	public void signal(WorkflowToken workflowToken, String transitionName,String result) {
		// TODO Auto-generated method stub
		WorkflowActivityInstance workflowActivityInstance=workflowActivityInstanceService.getById(workflowToken.getWorkflowActivityInstanceId());
		WorkflowActivity workflowActivity=workflowActivityService.getById(workflowActivityInstance.getWorkflowActivityId());
		WorkflowProcess workflowProcess=workflowProcessService.getById(workflowActivityInstance.getWorkflowProcessId());
		WorkflowDef workflowDef=workflowStore.get(workflowProcess.getWorkflowWorkflowId());
		NodeDef nodeDef=workflowDef.getNodeDefById(workflowActivity.getNodeId());
		INodeService nodeService=(INodeService)applicationContext.getBean(nodeDef.getType()+"NodeService");
		
		WorkflowContext workflowContext=new WorkflowContext();
		workflowContext.setWorkflowToken(workflowToken);
		workflowContext.setNodeDef(nodeDef);
		workflowContext.setWorkflowProcess(workflowProcess);
		workflowContext.setWorkflowActivityInstance(workflowActivityInstance);
		workflowContext.setWorkflowActivity(workflowActivity);
		workflowContext.setApproved(result);
		workflowContext.setBizKey(workflowProcess.getBizKey());
		nodeService.end(workflowContext,transitionName);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext=applicationContext;
	}
}